package com.jp.knowledge.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.jp.knowledge.R;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.base.RecycleVHolder;
import com.jp.knowledge.my.model.SearchResultModel;

import org.xutils.common.util.DensityUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/1/16.
 */

public class TeamCircleCompanySelectDialog extends Dialog {

    private SelectCompanyAdapter selectCompanyAdapter;

    public TeamCircleCompanySelectDialog(Context context) {
        super(context, R.style.custom_idalgo);

        View contentView = View.inflate(context, R.layout.dialog_team_circle_company_select, null);
        RecyclerView recycleView = (RecyclerView) contentView.findViewById(R.id.company_selected_rv);
        recycleView.setHasFixedSize(true);
        recycleView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        selectCompanyAdapter = new SelectCompanyAdapter(context, new ArrayList<SearchResultModel>());
        recycleView.setAdapter(selectCompanyAdapter);
        setContentView(contentView);

        getWindow().getAttributes().height = (int)(DensityUtil.getScreenHeight() * 0.6);
    }

    public void updateList(List<SearchResultModel> selectCompanys){
        selectCompanyAdapter.updateAdapter(selectCompanys);
    }

    private class SelectCompanyAdapter extends RecycleVAdapter<SearchResultModel> implements View.OnClickListener {
        public SelectCompanyAdapter(Context context, @NonNull List<SearchResultModel> list) {
            super(context, list);
        }
        @Override
        public int getLayoutId(int viewType) {
            return R.layout.dialog_team_circle_company_item;
        }
        @Override
        public void onBind(RecycleVHolder holder, int position) {
            holder.setTextView(R.id.company_name, getData(position).getName());
            View unSelectIcon = holder.get(R.id.unselect_company);
            unSelectIcon.setTag(position);
            unSelectIcon.setOnClickListener(this);
        }
        @Override
        public void onClick(View v) {
            if(iconClickListener != null)
                iconClickListener.unSelect((int) v.getTag());
        }
    }

    private IconClickListener iconClickListener;
    public void setIconClickListener(IconClickListener listener){
        this.iconClickListener = listener;
    }
    public interface IconClickListener{
        void unSelect(int position);
    }
}
